/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.ForecastEntity;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class MPSTableSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    
    @EJB
    private MonthlyDetailSessionBean mdsb = new MonthlyDetailSessionBean();
    @EJB
    private ForecastSessionBean fsb = new ForecastSessionBean();
    @EJB
    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
    
    public MPSTableEntity createMPSTableA(Date d){
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String test = sdf.format(d);
        System.out.println(test);
        try{
        d = sdf.parse(test);
        }
        catch(ParseException ex){
            
        }
        System.out.println(d.getTime());
        cal.setTime(d);
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 1, 0);
//        cal.set(Calendar.HOUR_OF_DAY, 0);
//        cal.set(Calendar.MINUTE, 1);
//        cal.set(Calendar.SECOND, 0);
        System.out.println(cal.getTime());
        MPSTableEntity mpsTable = new MPSTableEntity();
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDate(cal.getTime());
        System.out.println("MonthlyDetail:" + monthlyDetail.getScheduleA());
        String monthName = null;
        DateFormat df = new SimpleDateFormat("MMM");
        monthName = df.format(d);
        monthName = convertToEnglish(monthName);
        System.out.println(monthName);
        cal.setTime(d);
        int yearName = cal.get(Calendar.YEAR);
        yearName += 1900;
        System.out.println(yearName);
        ForecastEntity forecast = fsb.getForecastEntityByMonth(monthName, yearName);
        System.out.println("Forecast:"+forecast.getAmountA());
        mpsTable.setMonthlyDetailEntity(monthlyDetail);
        mpsTable.setForecastEntity(forecast);
        mpsTable.setMonthName(monthName);
        mpsTable.setYearName(yearName);
        mpsTable.setProductType("productA");
        int currentInventory = icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        System.out.println("CurrentInventory:" + currentInventory);
        if (monthName.equalsIgnoreCase("Jan")){
            mpsTable.setInventory(currentInventory - mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        else{
            Date lastMonth = d;
            cal.setTime(d);
            cal.add(Calendar.MONTH, -1);
            lastMonth = cal.getTime();
//            lastMonth.setMonth(d.getMonth()-1);
            MPSTableEntity lastMPS = getMPSTableByDate(lastMonth);
            mpsTable.setInventory(lastMPS.getInventory()- mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        mpsTable.setWorkingDay(monthlyDetail.getDays().size());
        return mpsTable;
    }
    
    public MPSTableEntity createMPSTableB(Date d){
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String test = sdf.format(d);
        System.out.println(test);
        try{
        d = sdf.parse(test);
        }
        catch(ParseException ex){
            
        }
        cal.setTime(d);
        cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 1, 0);
        MPSTableEntity mpsTable = new MPSTableEntity();
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDate(d);
        String monthName = null;
        DateFormat df = new SimpleDateFormat("MMM");
        monthName = df.format(d);
        monthName = convertToEnglish(monthName);
        int yearName = cal.get(Calendar.YEAR)+1900;
        ForecastEntity forecast = fsb.getForecastEntityByMonth(monthName, yearName);
        mpsTable.setMonthlyDetailEntity(monthlyDetail);
        mpsTable.setForecastEntity(forecast);
        mpsTable.setProductType("productB");
        int currentInventory = icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        if (monthName == "JAN"){
            mpsTable.setInventory(currentInventory - mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        else{
            Date lastMonth = d;
            cal.setTime(d);
            cal.add(Calendar.MONTH, -1);
            lastMonth = cal.getTime();
            //lastMonth.setMonth(d.getMonth()-1);
            MPSTableEntity lastMPS = getMPSTableByDate(lastMonth);
            mpsTable.setInventory(lastMPS.getInventory()- mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        return mpsTable;
    }
    
    public MPSTableEntity createMPSTableC(Date d){
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String test = sdf.format(d);
        System.out.println(test);
        try{
        d = sdf.parse(test);
        }
        catch(ParseException ex){
            
        }
        MPSTableEntity mpsTable = new MPSTableEntity();
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDate(d);
        String monthName = null;
        DateFormat df = new SimpleDateFormat("MMM");
        monthName = df.format(d);
        monthName = convertToEnglish(monthName);
        cal.setTime(d);
        int yearName = cal.get(Calendar.YEAR)+1900;
        ForecastEntity forecast = fsb.getForecastEntityByMonth(monthName, yearName);
        mpsTable.setMonthlyDetailEntity(monthlyDetail);
        mpsTable.setForecastEntity(forecast);
        mpsTable.setProductType("productC");
        int currentInventory = icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        if (monthName == "JAN"){
            mpsTable.setInventory(currentInventory - mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        else{
            Date lastMonth = d;
            cal.setTime(d);
            cal.add(Calendar.MONTH, -1);
            lastMonth = cal.getTime();
           // lastMonth.setMonth(d.getMonth()-1);
            MPSTableEntity lastMPS = getMPSTableByDate(lastMonth);
            mpsTable.setInventory(lastMPS.getInventory()- mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        return mpsTable;
    }
    
    public MPSTableEntity createMPSTableD(Date d){
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String test = sdf.format(d);
        System.out.println(test);
        try{
        d = sdf.parse(test);
        }
        catch(ParseException ex){
            
        }
        MPSTableEntity mpsTable = new MPSTableEntity();
        MonthlyDetailEntity monthlyDetail = mdsb.getMonthlyDetailByDate(d);
        String monthName = null;
        DateFormat df = new SimpleDateFormat("MMM");
        monthName = df.format(d);
        monthName = convertToEnglish(monthName);
        cal.setTime(d);
        int yearName = cal.get(Calendar.YEAR) +1900;
        ForecastEntity forecast = fsb.getForecastEntityByMonth(monthName, yearName);
        mpsTable.setMonthlyDetailEntity(monthlyDetail);
        mpsTable.setForecastEntity(forecast);
        mpsTable.setProductType("productD");
        int currentInventory = icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (monthName == "JAN"){
            mpsTable.setInventory(currentInventory - mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        else{
            Date lastMonth = d;
            cal.setTime(d);
            cal.add(Calendar.MONTH, -1);
            lastMonth = cal.getTime();
            //lastMonth.setMonth(d.getMonth()-1);
            MPSTableEntity lastMPS = getMPSTableByDate(lastMonth);
            mpsTable.setInventory(lastMPS.getInventory()- mpsTable.getForecastEntity().getAmountA() + mpsTable.getMonthlyDetailEntity().getScheduleA());
        }
        return mpsTable;
    }
    
    public MPSTableEntity getMPSTableByDate(Date d){
        Timestamp ts = new Timestamp(d.getTime());
        Query query = em.createQuery("SELECT u FROM MPSTableEntity u where u.ts = :dd");
        query.setParameter("dd", ts);
        MPSTableEntity dd = null;
        try {
            dd = (MPSTableEntity) query.getSingleResult();
        } catch (NoResultException ex) {
            throw ex;
        }
        return dd;
    }
    
    public String convertToEnglish(String in){
        String out = null;
        if (in.equalsIgnoreCase("一月")){
            out = "Jan";
        }
        else if (in.equalsIgnoreCase("二月")){
            out = "Feb";
        }
        else if (in.equalsIgnoreCase("三月")){
            out = "Mar";
        }
        else if (in.equalsIgnoreCase("四月")){
            out = "Apr";
        }
        else if (in.equalsIgnoreCase("五月")){
            out = "May";
        }
        else if (in.equalsIgnoreCase("六月")){
            out = "Jun";
        }
        else if (in.equalsIgnoreCase("七月")){
            out = "Jul";
        }
        else if (in.equalsIgnoreCase("八月")){
            out = "Aug";
        }
        else if (in.equalsIgnoreCase("九月")){
            out = "Sep";
        }
        else if (in.equalsIgnoreCase("十月")){
            out = "Oct";
        }
        else if (in.equalsIgnoreCase("十一月")){
            out = "Nov";
        }
        else if (in.equalsIgnoreCase("十二月")){
            out = "Dec";
        }
        return out;
    }
    
    public void saveMPSTable(MPSTableEntity table){
        em.persist(table);
    }
    
    public List<MPSTableEntity> getMPSTableByYear(int y){
        Query query = em.createQuery("SELECT f FROM ForecastTableEntity f");
        List<MPSTableEntity> mpsList = query.getResultList();
        List<MPSTableEntity> output = new ArrayList<MPSTableEntity>();
        for(MPSTableEntity mps: mpsList){
            if (mps.getMonth().getYear()==(y-1900)){
                output.add(mps);
            }
        }
        return output;
    }
    
}
